<template>
  <div class="form-wrapper">
    <header class="form-header">
      <BaseButton @click="goBack">
        <ChevronsLeft class="icon-base" />
      </BaseButton>
      <h1 class="page-title">默认备注设置</h1>
      <div style="width: 24px"></div>
    </header>

    <form class="form-container" @submit.prevent="updateNotes">
      <div class="form-group">
        <label class="form-label">备注</label>
        <textarea
          v-model="notes"
          class="form-input"
          placeholder="请输入默认备注内容"
          rows="4"
        ></textarea>
      </div>

      <button type="submit" class="submit-button">
        保存修改
      </button>
    </form>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ChevronsLeft } from 'lucide-vue-next';
import BaseButton from '@/components/BaseButton.vue';
import basicDetailIcon from '@/assets/icons/basic-detail.svg';
import axios from 'axios'

const router = useRouter();
const notes = ref('');
const icons = { basicDetail: basicDetailIcon };

const goBack = () => router.go(-1);

const updateNotes = async () => {
  if (!notes.value.trim()) {
    alert('备注内容不能为空');
    return;
  }

  const currentUser = JSON.parse(localStorage.getItem('user'));
  const userId = currentUser ? currentUser.uid : null;

  if (!userId) {
    alert('未找到当前用户，请重新登录');
    return;
  }

  try {
    const response = await axios.put('/api/auth/information/remarks',
      {
        uid: userId,
        remarks: notes.value
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': localStorage.getItem('token')
        }
      }
    );

    if (response.data.code === 0) {
      // 更新本地存储的用户信息
      currentUser.remarks = notes.value.trim();
      localStorage.setItem('user', JSON.stringify(currentUser));
      
      alert('默认备注修改成功');
      router.go(-1);
    } else {
      throw new Error(response.data.message || '修改失败');
    }
  } catch (error) {
    console.error('修改错误:', error);
    alert(error.response?.data?.message || error.message || '修改失败');
  }
};
</script>

<style scoped>
.form-wrapper {
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 16px;
}

.form-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
}

.btn-back {
  width: 40px;
  height: 40px;
  background: none;
  border: none;
  cursor: pointer;
}

.page-title {
  font-size: var(--font-size-large);
  font-weight: 600;
  color: #1a1a1a;
}

.form-container {
  max-width: 600px;
  margin: 0 auto;
  background: #ffffff;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  font-size: var(--font-size-normal);
  font-weight: 500;
  color: #333333;
  margin-bottom: 8px;
  display: inline-block;
}

.form-input {
  font-size: var(--font-size-normal);
  width: 100%;
  height: 48px;
  padding: 0 16px;
  color: #333333;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  background-color: #ffffff;
  transition: all 0.3s;
}

.form-input:focus {
  outline: none;
  border-color: #4f46e5;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.form-input::placeholder {
  color: #9ca3af;
}

.submit-button {
  width: 100%;
  padding: 15px;
  background-color: var(--primary-color);
  color: var(--text-light);
  border: none;
  border-radius: var(--border-radius-small);
  margin-top: 20px;
  cursor: pointer;
}



@media (max-width: 640px) {
  .form-container {
    padding: 20px;
  }

  .form-input {
    height: 44px;
  }

  .submit-button {
    height: 44px;
  }
}

.icon-base {
  width: 24px;
  height: 24px;
  color: #1a1a1a;
}
</style>
